package com.cookie.meetproject.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.Switch;
import android.widget.Toast;

import com.cookie.framework.base.BaseBackActivity;
import com.cookie.framework.bmob.BmobManger;
import com.cookie.framework.bmob.PrivateSet;
import com.cookie.framework.utils.CommonUtils;
import com.cookie.framework.view.LoadingView;
import com.cookie.meetproject.R;

import java.util.List;

import androidx.annotation.Nullable;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * 项目名:    MeetProject
 * 包名:      com.cookie.meetproject.ui
 * 文件名:    PrivateSetActivity
 * 创建者:    cookie
 * 创建时间:   2020/5/25 23:54
 * 描述:      TODO
 */

public class PrivateSetActivity extends BaseBackActivity implements View.OnClickListener {

    private Switch swKillContact;
    private LoadingView mLoadingView;

    /**
     * 私有库的创建
     * 1、创建一个bmobObject PrivateSet
     * 2、通过查询PrivateSet里面是否存在自己来判断开关
     * 3、开关的一些操作
     * 打开：则将自己添加到PrivateSet
     * 关闭：则将自己在PrivateSet中删除
     * 4、在查询联系人的时候通过查询PrivateSet过滤
     */

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_private_set);
        initView();
    }

    private void initView() {
        mLoadingView = new LoadingView(this);
        swKillContact = (Switch) findViewById(R.id.sw_kill_contact);
        swKillContact.setOnClickListener(this);
        queryPrivateSet();
    }

    //是否选中
    private boolean isCheck = false;
    //当前ID
    private String currentId = "";

    //查询私有库
    private void queryPrivateSet() {
        BmobManger.getInstance().queryPrivateSet(new FindListener<PrivateSet>() {
            @Override
            public void done(List<PrivateSet> list, BmobException e) {
                if (e == null) {
                    if (CommonUtils.isEmpty(list)) {
                        for (int i = 0; i < list.size(); i++) {
                            PrivateSet set = list.get(i);
                            if (set.getUserId().equals(BmobManger.getInstance().getUser().getObjectId())) {
                                currentId = set.getObjectId();
                                isCheck = true;
                                break;
                            }
                        }
                        swKillContact.setChecked(isCheck);
                    }
                }
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.sw_kill_contact:
                isCheck = !isCheck;
                swKillContact.setChecked(isCheck);
                if (isCheck) {
                    //添加
                    addPrivateSet();
                } else {
                    //删除
                    delPrivateSet();
                }
                break;
        }
    }

    //添加
    private void addPrivateSet() {
        mLoadingView.show("正在打开...");
        BmobManger.getInstance().addPrivateSet(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                mLoadingView.hide();
                if (e == null){
                    currentId = s;
                    Toast.makeText(PrivateSetActivity.this, "设置成功", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    //删除
    private void delPrivateSet() {
        mLoadingView.show("正在关闭...");
        BmobManger.getInstance().delPrivateSet(currentId, new UpdateListener() {
            @Override
            public void done(BmobException e) {
                mLoadingView.hide();
                if (e==null){
                    Toast.makeText(PrivateSetActivity.this, "关闭成功", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}
